package hn.cch.spring.bean.scope;

import hn.cch.spring.bean.scope.component.BeanSingleton;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
 * bean作用域跨域
 *
 */
@ComponentScan(value = {"hn.cch.spring.bean.scope.component"})
@Configuration
public class ScopeConfiguration {

    private static final Logger logger = LoggerFactory.getLogger(ScopeConfiguration.class);

    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext =
                new AnnotationConfigApplicationContext(ScopeConfiguration.class);
        BeanSingleton beanSingleton = applicationContext.getBean(BeanSingleton.class);

        logger.info("{}", beanSingleton.getBeanPrototype0().getClass());
        logger.info("{}", beanSingleton.getBeanPrototype1().getClass()); // 生成代理
        logger.info("{}", beanSingleton.getBeanPrototype2().getClass()); // 生成代理
        logger.info("{}", beanSingleton.getBeanPrototype3().getClass()); // DefaultListableBeanFactory$DependencyObjectProvider
        logger.info("{}", beanSingleton.getBeanPrototype4().getClass()); // 使用ApplicationContext生成prototype多例
        // 多次获取多例BeanPrototype0变成单例
        logger.info("{}", beanSingleton);
        logger.info("{}", beanSingleton);
        logger.info("{}", beanSingleton);
        logger.info("{}", beanSingleton);
    }
}
